package org.example.dao.impl;

import org.example.dao.UserDao;
import org.example.entity.User;
import org.example.utils.DbUtil;
import org.example.vo.UserVo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: StudentSystem
 * @BelongsPackage: org.example.dao.impl
 * @Author: StarBonny
 * @CreateTime: 2024-12-23  15:37
 * @Description: TODO
 * @Version: 1.0
 */
public class UserDaoImpl implements UserDao {
    @Override
    public List<User> showUser() {
        List<User> list = new ArrayList<>();
        String SQL = "SELECT user_id, name, pass, class_role_id, submit_date, status FROM [user]";
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection conn =  DbUtil.getConnection();
        try {
            pstmt = conn.prepareStatement(SQL);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setUserId(rs.getInt("user_id"));
                user.setName(rs.getString("name"));
                user.setPass(rs.getString("pass"));
                user.setClassRoleIdFk(rs.getInt("class_role_id"));
                user.setSubmitDate(rs.getDate("submit_date"));
                user.setStatus(rs.getInt("status"));
                list.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public List<UserVo> showUserPower() {
        List<UserVo> list = new ArrayList<>();
        String SQL = "SELECT ur.name,ur.pass,cs.name,cs.submit_date,mj.name,pt.name,ut.name FROM [user] ur JOIN class_role cr ON ur.class_role_id=cr.class_role_id JOIN position_type pt ON cr.position_type_id_fk=pt.position_type_id JOIN user_type ut ON pt.user_type_id_fk=ut.user_type_id JOIN class cs ON cr.class_id_fk=cs.class_id JOIN major mj ON cs.major_id_fk=mj.major_id";
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection conn =  DbUtil.getConnection();
        try {
            pstmt = conn.prepareStatement(SQL);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                UserVo user = new UserVo();
                user.setUserName(rs.getString("name"));
                user.setUserPass(rs.getString("pass"));
                user.setClassName(rs.getString("name"));
                user.setClassSubmitDate(rs.getDate("submit_date"));
                user.setMajorName(rs.getString("name"));
                user.setPositionTypeName(rs.getString("name"));
                user.setUserTypeName(rs.getString("name"));
                list.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
